package JZOffer;

import java.util.Stack;

public class _20 {
    public static void main(String[] args) {
        _20 c = new _20();
        _20.Solution s = c.getSolution();
        int[] pushA = {1, 2, 3, 4, 5};
//        int[] popA = {4, 5, 3, 2, 1};
        int[] popA = {4, 3, 5, 1, 2};
        System.out.println(s.IsPopOrder(pushA, popA));
    }

    Solution getSolution() {
        return new Solution();
    }

    class Solution {
        public boolean IsPopOrder(int[] pushA, int[] popA) {
//            ArrayList<Integer> pushAA = (ArrayList<Integer>) Arrays.stream(pushA).boxed().collect(Collectors.toList());
//            ArrayList<Integer> popAA = (ArrayList<Integer>) Arrays.stream(popA).boxed().collect(Collectors.toList());
            if (pushA.length == 0 || popA.length == 0)
                return false;
            Stack<Integer> stack = new Stack<Integer>();
//            stack.push(pushA[0]);
            int indexPop = 0;
            for (int i = 1; i < pushA.length; i++) {
                stack.push(pushA[i]);
                while (!stack.isEmpty() && stack.peek() == popA[indexPop]) {
                    stack.pop();
                    indexPop++;
                }
            }
            return stack.isEmpty();
        }
    }
}
